﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Windows.Forms;
using System.IO;

namespace lchealthy
{
    static class myfun
    {
        public static void DataToExcel(DataGridView m_DataView)
      {
          SaveFileDialog kk = new SaveFileDialog(); 
          kk.Title = "保存EXECL文件"; 
          kk.Filter = "EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*"; 
          kk.FilterIndex = 1;
          if (kk.ShowDialog() == DialogResult.OK) 
          { 
              string FileName = kk.FileName ;
              if (File.Exists(FileName))
                  File.Delete(FileName);
              FileStream objFileStream; 
              StreamWriter objStreamWriter; 
              string strLine = ""; 
              objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write); 
              objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
              for (int i = 0; i  < m_DataView.Columns.Count; i++) 
              { 
                  if (m_DataView.Columns[i].Visible == true) 
                  { 
                      strLine = strLine + m_DataView.Columns[i].HeaderText.ToString() + Convert.ToChar(9); 
                  } 
              } 
              objStreamWriter.WriteLine(strLine); 
              strLine = ""; 

              for (int i = 0; i  < m_DataView.Rows.Count; i++) 
              { 
                  if (m_DataView.Columns[0].Visible == true) 
                  { 
                      if (m_DataView.Rows[i].Cells[0].Value == null) 
                          strLine = strLine + " " + Convert.ToChar(9); 
                      else 
                          strLine = strLine + m_DataView.Rows[i].Cells[0].Value.ToString() + Convert.ToChar(9); 
                  } 
                  for (int j = 1; j  < m_DataView.Columns.Count; j++) 
                  { 
                      if (m_DataView.Columns[j].Visible == true) 
                      { 
                          if (m_DataView.Rows[i].Cells[j].Value == null) 
                              strLine = strLine + " " + Convert.ToChar(9); 
                          else 
                          { 
                              string rowstr = ""; 
                              rowstr = m_DataView.Rows[i].Cells[j].Value.ToString(); 
                              if (rowstr.IndexOf("\r\n") >  0) 
                                  rowstr = rowstr.Replace("\r\n", " "); 
                              if (rowstr.IndexOf("\t") >  0) 
                                  rowstr = rowstr.Replace("\t", " "); 
                              strLine = strLine + rowstr + Convert.ToChar(9); 
                          } 
                      } 
                  } 
                  objStreamWriter.WriteLine(strLine); 
                  strLine = ""; 
              } 
              objStreamWriter.Close(); 
              objFileStream.Close();
              MessageBox.Show("保存EXCEL成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); 
          }
      }


        //BMI
        public static System.Drawing.Color checkBMI(float BMI)
        {
            string s = "";
            if (BMI >= 27 && BMI <= 35)
            {
               return System.Drawing.Color.Yellow;
            }
            else if (BMI > 35)
            {
                return System.Drawing.Color.Red;
            }
            else 
            {
               return System.Drawing.Color.White;
            }
           
        }
        public static System.Drawing.Color checkzf(string sex, int age, float zf)
        {
            System.Drawing.Color c = System.Drawing.Color.White;
            if (sex == "男")
            {
                if (age >= 18 && age <= 39)
                {
                    if (zf >=17 && zf <= 26)
                        c = System.Drawing.Color.Yellow;
                    else if (zf >= 27 && zf <= 45)
                        c=System.Drawing.Color.Red;                  

                }
                else if (age >= 40 && age <= 59)
                {
                    if (zf >= 18 && zf <= 27)
                        c = System.Drawing.Color.Yellow;
                    else if (zf >= 28 && zf <= 45)
                        c = System.Drawing.Color.Red; 
                    

                }
                else
                {
                    if (zf >= 20 && zf <= 29)
                        c = System.Drawing.Color.Yellow;
                    else if (zf >= 30 && zf <= 45)
                        c = System.Drawing.Color.Red;   
                    

                }

            }
            else
            {
                if (age >= 18 && age <= 39)
                {
                    if (zf >= 28 && zf <= 39)
                        c = System.Drawing.Color.Yellow;
                    else if (zf >= 40 && zf <= 45)
                        c = System.Drawing.Color.Red;  
                    

                }
                else if (age >= 40 && age <= 59)
                {
                    if (zf >= 29 && zf <= 40)
                        c = System.Drawing.Color.Yellow;
                    else if (zf >= 41 && zf <= 45)
                        c = System.Drawing.Color.Red;   
                   

                }
                else
                {
                    if (zf >= 30 && zf <= 41)
                        c = System.Drawing.Color.Yellow;
                    else if (zf >= 42 && zf <= 45)
                        c = System.Drawing.Color.Red;   
                    

                }

            }
            return c;

        }
        //代谢差
        public static float getDX(float w, float dx)
        {
            return w* 24 - dx;

        }
        //BMI
        public static string getBMI(float BMI)
        {
            string s = "";
            if (BMI >= 18.5 && BMI <= 23)
            {
                s = "正常";
            }
            else if (BMI > 23 && BMI <= 27)
            {
                s = "超重";
            }
            else if (BMI > 27 && BMI <= 30)
            {
                s = "轻度肥胖";
            }
            else if (BMI > 30 && BMI <= 35)
            {
                s = "中度肥胖";
            }
            else if (BMI > 35)
            {
                s = "重度肥胖";
            }
            return s+" 正常18.5－23";
        }
        //标准体重
        public static float getWeight(float h, string sex)
        {
            if (h != 0)
            {
                if (sex == "男")
                {
                    return (h - 100) * 0.9f;
                }
                else
                {
                    return (h - 100) * 0.9f - 2.5f;
                }
            }
            else
                return 0;

        }
        //水分
        public static string getWater(string sex, float water)
        {
            string jg = "";
            if (sex == "男")
            {
                if (water < 55)
                    jg = "偏低。正常（55%－65%）";
                else if (water >= 55 && water <= 65)
                    jg = "正常（55%－65%）";
                else
                    jg = "偏高。正常（55%－65%）";
            }
            else
            {
                if (water < 50)
                    jg = "偏低。正常（50%－60%）";
                else if (water >= 50 && water <= 60)
                    jg = "正常（50%－60%）";
                else
                    jg = "偏高。正常（50%－60%）";

            }
            return jg;

        }
        //体脂肪
        public static string getZF(string sex, int age, float zf)
        {
            string jg = "";
            if (age < 18) return jg;
            if (sex == "男")
            {
                if (age >= 18 && age <= 39)
                {
                    if (zf >= 5 && zf <= 10)
                        jg = "偏瘦";
                    else if (zf >= 11 && zf <= 16)
                        jg = "健康型";
                    else if (zf >= 17 && zf <= 21)
                        jg = "警戒型";
                    else if (zf >= 22 && zf <= 26)
                        jg = "轻度肥";
                    else if (zf >= 27 && zf <= 45)
                        jg = "重度肥胖";
                    jg += "（正常是：11-16）";

                }
                else if (age >= 40 && age <= 59)
                {
                    if (zf >= 5 && zf <= 11)
                        jg = "偏瘦";
                    else if (zf >= 12 && zf <= 17)
                        jg = "健康型";
                    else if (zf >= 18 && zf <= 22)
                        jg = "警戒型";
                    else if (zf >= 23 && zf <= 27)
                        jg = "轻度肥";
                    else if (zf >= 28 && zf <= 45)
                        jg = "重度肥胖";
                    jg += "（正常是：12-17）";

                }
                else
                {
                    if (zf >= 5 && zf <= 13)
                        jg = "偏瘦";
                    else if (zf >= 14 && zf <= 19)
                        jg = "健康型";
                    else if (zf >= 20 && zf <= 24)
                        jg = "警戒型";
                    else if (zf >= 25 && zf <= 29)
                        jg = "轻度肥";
                    else if (zf >= 30 && zf <= 45)
                        jg = "重度肥胖";
                    jg += "（正常是：14-19）";

                }

            }
            else
            {
                if (age >= 18 && age <= 39)
                {
                    if (zf >= 5 && zf <= 20)
                        jg = "偏瘦";
                    else if (zf >= 21 && zf <= 27)
                        jg = "健康型";
                    else if (zf >= 28 && zf <= 34)
                        jg = "警戒型";
                    else if (zf >= 35 && zf <= 39)
                        jg = "轻度肥";
                    else if (zf >= 40 && zf <= 45)
                        jg = "重度肥胖";
                    jg += "（正常是：21-27）";

                }
                else if (age >= 40 && age <= 59)
                {
                    if (zf >= 5 && zf <= 21)
                        jg = "偏瘦";
                    else if (zf >= 22 && zf <= 28)
                        jg = "健康型";
                    else if (zf >= 29 && zf <= 35)
                        jg = "警戒型";
                    else if (zf >= 36 && zf <= 40)
                        jg = "轻度肥";
                    else if (zf >= 41 && zf <= 45)
                        jg = "重度肥胖";
                    jg += "（正常是：22-28）";

                }
                else
                {
                    if (zf >= 5 && zf <= 22)
                        jg = "偏瘦";
                    else if (zf >= 23 && zf <= 29)
                        jg = "健康型";
                    else if (zf >= 30 && zf <= 36)
                        jg = "警戒型";
                    else if (zf >= 37 && zf <= 41)
                        jg = "轻度肥";
                    else if (zf >= 42 && zf <= 45)
                        jg = "重度肥胖";
                    jg += "（正常是：23-29）";

                }

            }
            return jg;
        }
        //内脏脂肪
        public static string getNZ(int i)
        {
            string jg = "";
            if (i > 0 && i < 10)
            {
                jg = "(1-9):暂时不需要担心，请继续保持均衡的饮食和适量的运动吧。";
            }
            else if (i > 9 && i < 15)
            {
                jg = "(10-14):以健康适当的体重为目标，限制热量的摄入，注意坚持运动。";
            }
            else if (i > 14)
            {
                jg = "(15以上):积极地进行运动和限制饮食以减低内脏脂肪量，必要时请咨询专业医生。";
            }
            return jg;
        }
        //体型
        public static string getTX(int i)
        {
            string jg = "";
            switch (i)
            { 
                case 1:
                    jg = "隐形肥胖型";
                    break;
                case 2:
                    jg = "肥胖型";
                    break;
                case 3:
                    jg = "偏胖型";
                    break;
                case 4:
                    jg = "运动不足型";
                    break;
                case 5:
                    jg = "标准型";
                    break;
                case 6:
                    jg = "标准肌肉型";
                    break;
                case 7:
                    jg = "偏瘦型";
                    break;
                case 8:
                    jg = "偏瘦肌肉型";
                    break;
                case 9:
                    jg = "非常肌肉型";
                    break;
                default :
                    break;
            }
            return jg;
        }
        public static int getAge(DateTime dt)
        {
            return DateTime.Now.Year - dt.Year;
        }
        public static int getAge(string dt)
        {
            DateTime d;
            try
            {
                d = DateTime.Parse(dt);
            }
            catch (Exception)
            {
                
                return 0;
            }
            return DateTime.Now.Year - d.Year;
        }
        public static int getAge(DateTime dte,DateTime dtsr)
        {
            return dte.Year - dtsr.Year;
        }
        public static float getFloat(string n)
        {
            float a = 0;
            if (n.Trim() != "")
            {
                try
                {
                    a = float.Parse(n);
                }
                catch (Exception ex)
                {
                    
                }
            }
            return a;
        }
        public static int getInt(string n)
        {
            int a = 0;
            if (n.Trim() != "")
            {
                try
                {
                    a = int.Parse(n);
                }
                catch (Exception ex)
                {

                }
            }
            return a;
        }
        public static double getBMI(float h, float w)
        {
            return w/Math.Pow(h/100,2);
        }
        public static string GetChineseSpell(string strText)
        {
            int len = strText.Length;
            string myStr = "";
            for (int i = 0; i < len; i++)
            {
                myStr +=getSpell(strText.Substring(i, 1));
            }
            return myStr;
        }
        //5.25日由原来的private改为public
        public static string getSpell(string cnChar)
        {
            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
            if (arrCN.Length > 1)
            {
                int area = (short)arrCN[0];
                int pos = (short)arrCN[1];
                int code = (area << 8) + pos;
                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
                for (int i = 0; i < 26; i++)
                {
                    int max = 55290;
                    if (i != 25)
                        max = areacode[i + 1];
                    if (areacode[i] <= code && code < max)
                    {
                        return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                    }
                }
                return "*";
            }
            else
                return cnChar;
        }
        public static void Modify(string path, string key, string strvalue)
        {
            string xpath = "/myconfiguration/appSet/add[@key='?']";
            XmlDocument domwebconfig = new XmlDocument();

            domwebconfig.Load(path+"\\myconfig.xml");
            XmlNode addkey = domwebconfig.SelectSingleNode((xpath.Replace("?", key)));
            if (addkey == null)
            {
                throw new ArgumentException("没有找到<add key=" + key + " value=.../>的配置节");
            }
            addkey.Attributes["value"].InnerText = strvalue;
            domwebconfig.Save(path + "\\myconfig.xml");
        }
        public static string GetConfig(string key,string path)
        {
            string xpath = "/myconfiguration/appSet/add[@key='?']";
            XmlDocument domwebconfig = new XmlDocument();

            domwebconfig.Load(path + "\\myconfig.xml");
            XmlNode addkey = domwebconfig.SelectSingleNode((xpath.Replace("?", key)));
            if (addkey == null)
            {
                throw new ArgumentException("没有找到<add key=" + key + " value=.../>的配置节");
            }
            return addkey.Attributes["value"].InnerText;
            //domwebconfig.Save(HttpContext.Current.Server.MapPath("/web.config"));

        }

    }
}
